import 'package:flutter/material.dart';

class MsgInputWidget extends StatefulWidget {
  final String hint;
  final Function(String)? onChange;
  final VoidCallback? onSend;
  final int maxLength;
  final int maxLines;

  const MsgInputWidget(
    this.hint, {
    super.key,
    this.onChange,
    this.onSend,
    this.maxLength = 1000,
    this.maxLines = 4,
  });

  @override
  State<MsgInputWidget> createState() => _MsgInputWidgetState();
}

class _MsgInputWidgetState extends State<MsgInputWidget> {
  final TextEditingController _controller = TextEditingController();
  bool _hasInput = false;

  @override
  void initState() {
    super.initState();
    _controller.addListener(() {
      setState(() {
        _hasInput = _controller.text.trim().isNotEmpty;
      });
    });
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
      decoration: BoxDecoration(
        color: Theme.of(context).cardColor,
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.05),
            offset: const Offset(0, -1),
            blurRadius: 3,
          ),
        ],
      ),
      child: SafeArea(
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.end,
          children: [
            Expanded(
              child: Container(
                constraints: BoxConstraints(maxHeight: 36.0 * widget.maxLines),
                decoration: BoxDecoration(
                  color:
                      Theme.of(context).brightness == Brightness.light
                          ? Colors.grey[200]
                          : Colors.grey[800],
                  borderRadius: BorderRadius.circular(18),
                ),
                child: TextField(
                  controller: _controller,
                  maxLength: widget.maxLength,
                  maxLines: widget.maxLines,
                  minLines: 1,
                  decoration: InputDecoration(
                    hintText: widget.hint,
                    hintStyle: TextStyle(
                      color: Theme.of(context).hintColor,
                      fontSize: 14,
                    ),
                    contentPadding: const EdgeInsets.symmetric(
                      horizontal: 16,
                      vertical: 8,
                    ),
                    border: InputBorder.none,
                    counterText: '',
                  ),
                  style: const TextStyle(fontSize: 14),
                  onChanged: (value) {
                    widget.onChange?.call(value);
                  },
                  onSubmitted:
                      _hasInput
                          ? (value) {
                            _sendMessage();
                          }
                          : null,
                ),
              ),
            ),
            const SizedBox(width: 8),
            _buildSendButton(),
          ],
        ),
      ),
    );
  }

  Widget _buildSendButton() {
    return Material(
      color: Colors.transparent,
      child: InkWell(
        onTap: _hasInput ? _sendMessage : null,
        borderRadius: BorderRadius.circular(24),
        child: Container(
          width: 48,
          height: 36,
          decoration: BoxDecoration(
            color:
                _hasInput
                    ? Theme.of(context).primaryColor
                    : Theme.of(context).disabledColor,
            borderRadius: BorderRadius.circular(18),
          ),
          child: Icon(
            Icons.send,
            color: _hasInput ? Colors.white : Colors.grey[400],
            size: 20,
          ),
        ),
      ),
    );
  }

  void _sendMessage() {
    if (_controller.text.trim().isEmpty) return;
    widget.onSend?.call();
    _controller.clear();
  }
}
